package chat.whiteboard;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import chat.whiteboard.components.ArrowButton;
import chat.whiteboard.components.ChatInputTextField;
import chat.whiteboard.components.ChatTextArea;
import chat.whiteboard.components.CircleButton;
import chat.whiteboard.components.FontComboBox;
import chat.whiteboard.components.LineButton;
import chat.whiteboard.components.RectangleButton;
import chat.whiteboard.components.SendButton;
import chat.whiteboard.components.canvas.JCanvas;
import chat.whiteboard.components.legend.LegendList;
import chat.whiteboard.draw.elements.Arrow;
import chat.whiteboard.draw.elements.Circle;
import chat.whiteboard.draw.elements.Drawing;
import chat.whiteboard.draw.elements.Line;
import chat.whiteboard.draw.elements.Rectangle;
import chat.whiteboard.draw.state.DrawingStateManager;
import chat.whiteboard.models.DrawingModel;
import chat.whiteboard.models.UserModel;
import chat.whiteboard.utils.classes.CustomColor;



public class WhiteBoardController implements IWhiteBoardController{
	private RectangleButton rectBtn;
	private CircleButton	circleBtn;
	private ArrowButton		arrowBtn;
	private LineButton		lineBtn;
	private SendButton		sendBtn;
	private JCanvas			canvas;
	private JComboBox		colorComboBox;
	private ChatInputTextField tfChatInput;
	private ChatTextArea	chatTextArea;
	private FontComboBox	cbFont;
	private DrawingStateManager 	stMng;
	private LegendList		legend_list;
	private UserModel 		current_user = null;

	public WhiteBoardController(){
		stMng = new DrawingStateManager(this);
	}
	//<Start States>
	public void startRectangleState(){
		stMng.setRectangleState();
	}
	public void startCircleState(){
		stMng.setCircleState();		
	}
	public void startArrowState(){
		stMng.setArrowState();
	}
	public void startLineState(){
		stMng.setLineState();
	}
	//<End States>
	//<Start-Registration>
	public void registerRectangleButton(RectangleButton btn){
		this.rectBtn = btn;
	}
	public void registerCircleButton(CircleButton btn){
		this.circleBtn = btn;
	}
	public void registerArrowButton(ArrowButton btn){
		this.arrowBtn = btn;
	}
	public void registerLineButton(LineButton btn){
		this.lineBtn = btn;
	}
	
	public void registerSendButton(SendButton btn){
		this.sendBtn = btn;
	}
	public void registerCanvas(JCanvas canvas){
		this.canvas = canvas;
	}
	public void registerColorComboBox(JComboBox cb){
		this.colorComboBox = cb;
	}
	
	public void registerTextFieldChatInput(ChatInputTextField chat_input){
		this.tfChatInput = chat_input;
	}
	
	public void registerChatTextArea(ChatTextArea textArea){
		this.chatTextArea = textArea;
	}
	public void registerFontComboBox(FontComboBox cbFont){
		this.cbFont = cbFont;
	}
	public void registerLegenList(LegendList legend_list){
		this.legend_list = legend_list;
	}
	public void sendInputText(){
		// set parameters
		Color color = ((CustomColor)colorComboBox.getModel().getSelectedItem()).getColor();
		Integer font_size = (Integer)cbFont.getModel().getSelectedItem();
		String text = current_user.get_text()+" : " + tfChatInput.getText();
		// call
		addTextToChat(text, font_size, color);
		tfChatInput.setText("");
	}
	public void setDrawingColor(Color color){
		stMng.setColor(color);
	}
	public void setFont(Font c_font){
		
	}
	public void draw(int x, int y){
		stMng.mouseDown(x, y);
		canvas.repaint();
	}
	@Override
	public void addDrawing(Drawing d){
		canvas.addDrawing(d);
		canvas.repaint();
	}
	//<End-Registration>
	@Override
	public void addTextToChat(String text,Integer font_size, Color color){
        StyledDocument doc = chatTextArea.getStyledDocument();

        Style style = chatTextArea.addStyle("I'm a Style", null);
        StyleConstants.setForeground(style, color);
        StyleConstants.setFontSize(style, font_size);
         
        try {
            doc.insertString(doc.getLength(), text + "\n", style);
        } catch (BadLocationException ex) {
        	ex.printStackTrace();
        }				
	}
	@Override
	public void addUser(UserModel user) {
		DefaultListModel<UserModel> model = (DefaultListModel<UserModel>)legend_list.getModel();
		model.addElement(user);
	}
	@Override
	public void setCurrentUser(UserModel user) {
		current_user = user;
		addUser(user);
		stMng.setColor(user.get_color());
	}
}
